package com.trainoo.alibaba.nacos;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: zt
 * @Date: 2019/12/12 11:20
 * @Description:
 */
@EnableFeignClients("org.trainoo.feign.api")
@EnableDiscoveryClient
@SpringBootApplication
public class NacosDiscoveryClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosDiscoveryClientApplication.class);
    }

    // ==== 方式 1 使用LoadBalance
    @Slf4j
    @RestController
    static class TestController1{

        @Autowired
        private LoadBalancerClient loadBalancerClient;

        @GetMapping("/test1")
        public String test(){
            ServiceInstance serviceInstance = loadBalancerClient.choose("alibaba-nacos-discovery-server");
            String url = serviceInstance.getUri() + "/hello";
            RestTemplate restTemplate = new RestTemplate();
            String result = restTemplate.getForObject(url, String.class);
            return "[Using LoadBalance]Invoke: [" + url + "], return: [" + result + "]";
        }
    }

    // ==== 方式 2 使用RestTemplate
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    @Slf4j
    @RestController
    static class TestController2 {

        @Autowired
        RestTemplate restTemplate;

        @GetMapping("/test2")
        public String test() {
            String result = restTemplate.getForObject("http://alibaba-nacos-discovery-server/hello", String.class);
            return "[Using RestTemplate]Return : " + result;
        }
    }

    // ==== 方式 3 使用WebClient --- WebClient是Spring 5中最新引入的
    // @Bean
    // @LoadBalanced
    // public WebClient.Builder loadBalancedWebClientBuilder() {
    //     return WebClient.builder();
    // }
    // @Slf4j
    // @RestController
    // static class TestController3 {
    //
    //     @Autowired
    //     private WebClient.Builder webClientBuilder;
    //
    //     @GetMapping("/test3")
    //     public Mono<String> test() {
    //         Mono<String> result = webClientBuilder.build()
    //                 .get()
    //                 .uri("http://alibaba-nacos-discovery-server/hello?name=didi")
    //                 .retrieve()
    //                 .bodyToMono(String.class);
    //         return result;
    //     }
    // }
}
